﻿using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
namespace PasteLoggerBoard.usermodels
{
    /// <summary>
    /// 接收配置
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "root", "root" })]
    public class WebHookLinkAppService : PasteLoggerBoardAppService
    {

        private readonly IPasteLoggerBoardDbContext _dbContext;
        public WebHookLinkAppService(IPasteLoggerBoardDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 按页获取接收配置
        ///</summary>
        /// <param name="page">页码</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<WebHookLinkListDto>> GetListAsync(int page = 1, int size = 20)
        {

            var query = _dbContext.WebHookLink.Where(t => 1 == 1).OrderByDescending(xy => xy.Id);
            var pagedResultDto = new PagedResultDto<WebHookLinkListDto>();
            pagedResultDto.TotalCount = await query.CountAsync();
            var userList = await query.Page(page, size).ToListAsync();
            var temList = ObjectMapper.Map<List<WebHookLink>, List<WebHookLinkListDto>>(userList);
            pagedResultDto.Items = temList;
            return pagedResultDto;
        }

        /// <summary>
        /// 根据ID获取单项接收配置
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public WebHookLinkDto GetByIdAsync(int id)
        {
            var query = _dbContext.WebHookLink.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<WebHookLink, WebHookLinkDto>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取待更新单项信息接收配置
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public WebHookLinkUpdateDto GetInfoForUpdateAsync(int id)
        {
            var query = _dbContext.WebHookLink.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<WebHookLink, WebHookLinkUpdateDto>(query);
            return temList;
        }


        /// <summary>
        /// 添加一个接收配置
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebHookLinkDto> CreateItemAsync(WebHookLinkAddDto input)
        {
            var newu = ObjectMapper.Map<WebHookLinkAddDto, WebHookLink>(input);
            _dbContext.Add(newu);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<WebHookLink, WebHookLinkDto>(newu);
            return backinfo;
        }
        /// <summary>
        /// 更新一个接收配置
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebHookLinkDto> UpdateItemAsync(WebHookLinkUpdateDto input)
        {
            var info = await _dbContext.WebHookLink.Where(x => x.Id == input.Id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new UserFriendlyException("需要查询的信息不存在", "404");
            }
            ObjectMapper.Map<WebHookLinkUpdateDto, WebHookLink>(input, info);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<WebHookLink, WebHookLinkDto>(info);
            return backinfo;
        }
    }
}
